<?php

/**
 * This is the model class for table "{{mem_fav}}".
 *
 * The followings are the available columns in table '{{mem_fav}}':
 * @property integer $zmf_id
 * @property integer $zm_id
 * @property integer $zp_id
 * @property integer $zmf_type
 * @property integer $inputtime
 */
class MemFav extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return MemFav the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{mem_fav}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zm_id, zp_id, zmf_type, inputtime', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zmf_id, zm_id, zp_id, zmf_type, inputtime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'product'=>array(self::BELONGS_TO,'Product','zp_id'),
			'company'=>array(self::BELONGS_TO,'Company',array('zp_id'=>'zm_id')),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zmf_id' => 'ID',
			'zm_id' => '会员ID',
			'zp_id' => '产品/商铺ID',
			'zmf_type' => '类型',
			'inputtime' => '创建时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zmf_id',$this->zmf_id);
		$criteria->compare('zm_id',$this->zm_id);
		$criteria->compare('zp_id',$this->zp_id);
		$criteria->compare('zmf_type',$this->zmf_type);
		$criteria->compare('inputtime',$this->inputtime);

		$criteria->order = " inputtime DESC ";//默认为时间倒序
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	/**
	 * 添加收藏
	 * @param $id 产品OR商铺ID
	 * @param $type 类型: 1.产品 ，2商铺,默认为产品
	 * */
	public function saveFavorites($zpid,$type=1){
		$zm_id = Yii::app()->user->getId();
		//如果已经收藏过，返回false;
		$moel = $this->findByAttributes(array('zm_id'=>$zm_id,'zp_id'=>$zpid,'zmf_type'=>$type));
		if(!empty($moel))
			return false;
		//如果是子帐户，返回false;
		if(Yii::app()->usertype=="sub_member")
			return false;
		
		$this->__construct('insert');
		$this->zm_id = $zm_id;
		$this->zp_id = $zpid;
		$this->zmf_type = $type;
		$this->inputtime = time();
		$data = array('zm_id','zp_id','zmf_type','inputtime');
		$this->insert($data);
		//更新产品收藏次数
		if($type==1){
			Product::model()->updateByPk($zpid,array('zp_fav_num'=>'zp_fav_num'+1));
		}
		return true;
	}
	/**
	 * 查找登录会员收藏的产品
	 * */
	public function findFoverProduct(){
		$criteria=new CDbCriteria;
		$criteria->compare('zm_id',Yii::app()->user->getId());
		$criteria->compare('zmf_type','1');
		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
		));
	}
	/**
	 * 查找登录会员收藏的商铺
	 * */
	public function findFoverConpany(){
		$criteria=new CDbCriteria;
		$criteria->compare('zm_id',Yii::app()->user->getId());
		$criteria->compare('zmf_type','2');
		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
		));
	}
	/**
	 * 查找登录会员收藏的产品，返回数组
	 * */
	public function getFoverProduct($pageSize=10){
		$criteria=new CDbCriteria;
		$criteria->with = 'product';
		$criteria->compare('t.zm_id',Yii::app()->user->getId());
		$criteria->compare('zmf_type',1);
		$amount = $this->count($criteria);
		$pager = new CPagination($amount);
		$pager->pageSize = $pageSize;
		$criteria->offset = $pager->currentPage*$pager->pageSize;
		$criteria->limit = $pager->pageSize;
		//print_r($criteria);exit;
		$data = $this->findAll($criteria);
		return array('pager'=>$pager,'data'=>$data);
	}
	/**
	 * 查找登录会员收藏的店铺，返回数组
	 * */
	public function getFoverShop($pageSize=10){
		$criteria=new CDbCriteria;
		//$criteria->with = 'company';
		$criteria->compare('t.zm_id',Yii::app()->user->getId());
		$criteria->compare('zmf_type',2);
		//分页
		$amount = $this->count($criteria);
		$pager = new CPagination($amount);
		$pager->pageSize = $pageSize;
		$criteria->offset = $pager->currentPage*$pager->pageSize;
		$criteria->limit = $pager->pageSize;
		
		$data = $this->findAll($criteria);
		foreach($data as $key=>$value){
			$newData[$key] = $value->attributes;
			$newData[$key]['company'] = Company::model()->getCompanyByMemberId($value['zp_id']);
			$newData[$key]['company']['newProduct'] = Product::model()->getNewProductByMemberId($value['zp_id']);
		}
		//print_r($newData);exit;
		return array('pager'=>$pager,'data'=>$newData);
	}
	/**
	 * 删除收藏
	 * $idbox 数组
	 * $memberid 会员id
	 * */
	public function deleteFav($idbox, $memberid=null){
		$condition = 'zmf_id in('.implode(',', $idbox).')';
		if($memberid){
			$condition .= ' and zm_id='.$memberid;
		}
		return $this->deleteAll($condition);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}